package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.RoleAuthority;
import com.company.project.service.RoleAuthorityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/role/authority")
public class RoleAuthorityController {
    @Resource
    private RoleAuthorityService roleAuthorityService;

    /**
     * 删除角色权限
     * @return
     */
    @GetMapping("/delete/role/{rid}")
    @ResponseBody
    public Result deleteRoleAuthority(@PathVariable("rid") Integer rid){
        Boolean aBoolean = roleAuthorityService.deleteRoleAuthority(rid);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 添加权限
     * @param roleAuthority
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody RoleAuthority roleAuthority) {
        Boolean aBoolean = roleAuthorityService.addRoleAuthority(roleAuthority);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 删除并更新角色权限
     * @param roleAuthority
     * @return
     */
    @PostMapping("/updateRole")
    @ResponseBody
    public Result deleteRoleA(@RequestBody RoleAuthority roleAuthority){
        Boolean aBoolean = roleAuthorityService.deleteRoleA(roleAuthority);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        roleAuthorityService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(RoleAuthority roleAuthority) {
        roleAuthorityService.update(roleAuthority);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        RoleAuthority roleAuthority = roleAuthorityService.findById(id);
        return ResultGenerator.genSuccessResult(roleAuthority);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<RoleAuthority> list = roleAuthorityService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
